package cn.edu.njnu.opengmpback.resource.controller;

import cn.edu.njnu.opengmpback.common.utils.JsonResult;
import cn.edu.njnu.opengmpback.common.utils.ResultUtils;
import cn.edu.njnu.opengmpback.resource.entity.ResourceData;
import cn.edu.njnu.opengmpback.resource.entity.UserData;
import cn.edu.njnu.opengmpback.resource.entity.dto.*;
import cn.edu.njnu.opengmpback.resource.service.ResCenterService;
import cn.edu.njnu.opengmpback.user.entity.UserVo;
import cn.edu.njnu.opengmpback.user.service.UserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping(value = "/centerRes")
public class ResCenterController {

    @Autowired
    ResCenterService resCenterService;

    @Autowired
    UserService userService;

    //    @LoginRequired
    @ApiOperation(value = "数据---创建资源中心数据")
    @PostMapping("/saveResourceData")
    public JsonResult saveResourceData(@RequestPart("info") CreateResourceDataDTO createResourceDataDTO,
                                       @ApiParam @RequestPart(value = "visualFile",required = false) MultipartFile visualFile/*,
                                       @RequestPart("imgFile") MultipartFile imgFile*/){
        return resCenterService.saveResourceData(createResourceDataDTO,visualFile/*,imgFile*/);
    }

    //    @LoginRequired
    @ApiOperation(value = "数据---获取资源中心数据列表")
    @PostMapping("/getResourceDataList")
    public JsonResult getResourceDataList(@RequestBody ResourcePageDTO resourcePageDTO){
        return resCenterService.getResourceDataList(resourcePageDTO);
    }

    @ApiOperation(value = "数据---删除资源中心数据")
    @GetMapping("/deleteResourceData/{resourceDataId}")
    public JsonResult deleteResourceData(@PathVariable("resourceDataId") String resourceDataId ){
        return resCenterService.deleteResourceData(resourceDataId);
    }

    @ApiOperation(value = "数据---保存资源中心其他小数据")
    @PostMapping("/saveResourceSmallFile")
    public JsonResult saveResourceSmallFile(@RequestPart("info") CreateResourceSmallFileDTO createResourceSmallFileDTO,
                                            @RequestPart("smallFile") MultipartFile smallFile){
        return resCenterService.saveResourceSmallFile(createResourceSmallFileDTO,smallFile);
    }

    @ApiOperation(value = "数据---保存资源中心条目数据")
    @PostMapping("/saveResourceItemFile")
    public JsonResult saveResourceItemFile(@RequestPart("info") CreateResourceSmallFileDTO createResourceSmallFileDTO,
                                           @RequestPart("imgFile") MultipartFile imgFile){
        return resCenterService.saveResourceItemFile(createResourceSmallFileDTO,imgFile);
    }


    @ApiOperation(value = "数据---删除资源中心其他小数据")
    @GetMapping("/deleteResourceSmall/{resourceSmallFileId}")
    public JsonResult deleteResourceSmallFile(@PathVariable("resourceSmallFileId") String resourceSmallFileId ){
        return resCenterService.deleteResourceSmallFile(resourceSmallFileId);
    }

    @ApiOperation(value = "数据---检索public数据列表")
    @PostMapping("/getPublicUserDataList")
    public JsonResult getPublicUserDataList(@RequestBody ResourcePageDTO resourcePageDTO){
        return resCenterService.getPublicUserDataList(resourcePageDTO);
    }

    @ApiOperation(value = "数据---保存public数据到个人空间")
    @PostMapping("/saveUserFiletoUserSpace")
    public JsonResult saveUserFiletoUserSpace(@RequestParam("folderName") String folderName, @RequestBody List<UserData> DataList, HttpServletRequest request){
        Object userState = userService.userState(request);
        if(userState.equals(false)){
            return ResultUtils.error(-1,"用户未登录");
        }
        UserVo userVo = (UserVo)userState;
        String userId = userVo.getUserId();
        return resCenterService.saveUserFiletoUserSpace(folderName, DataList, userId);
    }

    @ApiOperation(value = "模型---创建资源中心的模型条目")
    @PostMapping("/createResourceModel")
    public JsonResult createResourceModel(@RequestPart("info") CreateResourceModelDTO createResourceModelDTO,
                                          @RequestPart("imgFile") MultipartFile imgFile){
        return resCenterService.createResourceModel(createResourceModelDTO,imgFile);
    }

    @ApiOperation(value = "模型---从门户模型条目中提取模型")
    @GetMapping("/getFromPortalModel")
    public JsonResult getFromPortalModel(){
        return resCenterService.getFromPortalModel();
    }

    @ApiOperation(value = "方法---提取条目")
    @GetMapping("/getFromMethod")
    public JsonResult getFromMethod(){
        return resCenterService.getFromMethod();
    }

    @ApiOperation(value = "模型---获取资源中心模型列表")
    @PostMapping("/getResourceModelList")
    public JsonResult getResourceModelList(@RequestBody ResourcePageDTO resourcePageDTO){
        return resCenterService.getResourceModelList(resourcePageDTO);
    }

    @ApiOperation(value = "方法---创建资源中心的方法条目")
    @PostMapping("/createResourceMethod")
    public JsonResult createResourceMethod(@RequestPart("info") CreateResourceMethodDTO createResourceMethodDTO,
                                           @RequestPart("imgFile") MultipartFile imgFile){
        return resCenterService.createResourceMethod(createResourceMethodDTO,imgFile);
    }

    @ApiOperation(value = "方法---获取资源中心方法列表")
    @PostMapping("/getResourceMethodList")
    public JsonResult getResourceMethodList(@RequestBody ResourcePageDTO resourcePageDTO){
        return resCenterService.getResourceMethodList(resourcePageDTO);
    }

    @ApiOperation(value = "方法/模型---获取资源中心方法/模型根据id")
    @PostMapping("/getResourceModelById/{resourceModelId}")
    public JsonResult getResourceModelById(@PathVariable("resourceModelId") String resourceModelId){
        return resCenterService.getResourceModelById(resourceModelId);
    }

    @ApiOperation(value = "方法/模型---删除资源中心的模型/模型条目")
    @GetMapping("/deleteResourceModel/{resourceModelId}")
    public JsonResult deleteResourceModel(@PathVariable("resourceModelId") String resourceModelId){
        return resCenterService.deleteResourceModel(resourceModelId);
    }

    @ApiOperation(value = "方法/模型---保存方法/模型到个人空间")
    @PostMapping("/saveModeltoUserSpace/{id}")
    public JsonResult saveModeltoUserSpace(@PathVariable("id") String id, HttpServletRequest request){
        Object userState = userService.userState(request);
        if(userState.equals(false)){
            return ResultUtils.error(-1,"用户未登录");
        }
        UserVo userVo = (UserVo)userState;
        String userId = userVo.getUserId();
        return resCenterService.saveModeltoUserSpace(id, userId);
    }

    @ApiOperation(value = "方法/模型---保存方法/模型到计算模型的工作空间")
    @PostMapping("/saveModeltoWorkSpace/{id}/{modelId}")
    public JsonResult saveModeltoWorkSpace(@PathVariable("id") String id,@PathVariable("modelId") String modelId, HttpServletRequest request){
        Object userState = userService.userState(request);
        if(userState.equals(false)){
            return ResultUtils.error(-1,"用户未登录");
        }
        UserVo userVo = (UserVo)userState;
        String userId = userVo.getUserId();
        return resCenterService.saveModeltoWorkSpace(id, modelId, userId);
    }

    @ApiOperation(value = "数据---保存数据到个人空间")
    @PostMapping("/saveFiletoUserSpace")
    public JsonResult saveFiletoUserSpace(@RequestParam("folderName") String folderName, @RequestBody List<ResourceData> resourceDataList, HttpServletRequest request){
        Object userState = userService.userState(request);
        if(userState.equals(false)){
            return ResultUtils.error(-1,"用户未登录");
        }
        UserVo userVo = (UserVo)userState;
        String userId = userVo.getUserId();
        return resCenterService.saveFiletoUserSpace(folderName, resourceDataList, userId);
    }

    @ApiOperation(value = "数据---浏览数据，pageviews加一")
    @GetMapping("/resDataView/{resDataId}")
    public JsonResult resDataView(@PathVariable("resDataId") String id){
        return resCenterService.resDataView(id);
    }

    @ApiOperation(value = "数据---获取点击量前六的数据条目")
    @GetMapping("/getResByDataView")
    public JsonResult getResByDataView(){
        return resCenterService.getResByDataView();
    }

    @GetMapping("/queryModelItemFuzzy/{searchText}")
    @ApiOperation(value = "模糊查询工具")
    public JsonResult queryModelItemFuzzy(@PathVariable(value = "searchText")String searchText) {
        return resCenterService.queryModelItemFuzzy(searchText);
    }

    @GetMapping("/getResourceMethodByName/{searchText}")
    @ApiOperation(value = "方法查询工具")
    public JsonResult getResourceMethodByName(@PathVariable(value = "searchText")String searchText) {
        return resCenterService.queryResourceMethodFuzzy(searchText);
    }

}
